// pages/home/home.js

//获取应用实例
const app = getApp();

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    jump:0,  //判断正常进入还是从游戏页面跳过来授权
    jump_dice:0,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    navigatorList: [{
        url: "/pages/undercover/pages/index/index",
        img: "../../../images/home_undercover.png",
        name: "谁是卧底",
        goto: 'goToRoom1'
      },
      {
        url: "/pages/guessword/pages/index/index",
        img: "../../../images/home_guess.png",
        name: "你比我猜",
        goto: 'goToRoom2'
      },
      {
        url: "/pages/shakedice/pages/index/index",
        img: "../../../images/home_dice.png",
        name: "掷骰子",
        goto: 'goToRoom3'
      },
      // {
      //   url: "/pages/shakedice/pages/index/index",
      //   img: "../../../images/home_prize.png",
      //   name: "抽奖",
      //   goto: 'goToRoom3'
      // }
    ]
  },
  onLoad: function () {
    var that = this;
    var pages = getCurrentPages() //获取加载的页面
    var currentPage = pages[pages.length - 1] //获取当前页面的对象
    var url = currentPage.route //当前页面url
    var options = currentPage.options //如果要获取url中所带的参数可以查看options
    if (typeof (options.jump) != 'undefined') {
      this.setData({
        jump: options.jump,
      })
    }
    if (typeof (options.jump_dice) != 'undefined') {
      this.setData({
        jump_dice: options.jump_dice,
      })
    }
    if (app.globalData.userInfo) {
      that.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
      //谁是卧底    如果路径是从游戏页面调过来授权的
      if (typeof (options.jump) != 'undefined') { //分享链接进入的
        wx.navigateTo({
          url: '/pages/undercover/pages/index/index?room_id=' + that.data.jump + '&type=2',
        })
      }
      //掷色子    如果路径是从游戏页面调过来授权的
      if (typeof (options.jump_dice) != 'undefined') { //分享链接进入的
        wx.navigateTo({
          url: '/pages/shakedice/pages/index/index?room_id=' + that.data.jump_dice + '&type=2',
        })
      }
    } else if (that.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        that.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        //已获取授权后登录
        wx.request({
          url: app.globalData.path + 'login',
          method: 'get',
          data: {
            openid: app.globalData.openid,
            nickname: that.data.userInfo.nickName,
            avatar_url:that.data.userInfo.avatarUrl
          },
          dataType: 'json',
          success: res => {
            app.globalData.session_id = res.data.data.session_id;
            app.globalData.userInfoSession = res.data.data;
            // // 如果路径是从游戏页面调过来授权的
            if (typeof (options.jump) != 'undefined') { //分享链接进入的
              wx.navigateTo({
                url: '/pages/undercover/pages/index/index?room_id=' + that.data.jump + '&type=2',
              })
            }
            //掷色子    如果路径是从游戏页面调过来授权的
            if (typeof (options.jump_dice) != 'undefined') { //分享链接进入的
              wx.navigateTo({
                url: '/pages/shakedice/pages/index/index?room_id=' + that.data.jump_dice + '&type=2',
              })
            }
          }
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
//未获取授权时获取授权并登录
  getUserInfo: function(e) {
    var that = this
    if (e.detail.userInfo) {
      wx.setStorageSync('userInfo', e.detail.userInfo)
      app.globalData.userInfo = e.detail.userInfo
      //
      wx.request({
        url: app.globalData.path + 'login',
        method: 'get',
        data: {
          openid: app.globalData.openid,
          nickname: e.detail.userInfo.nickName,
          avatar_url: e.detail.userInfo.avatarUrl
        },
        dataType: 'json',
        success: res => {
          app.globalData.session_id = res.data.data.session_id;
          app.globalData.userInfoSession = res.data.data;
          // 如果路径是从游戏页面调过来授权的
          if(that.data.jump != ''){ //分享链接进入的
            wx.navigateTo({
              url: '/pages/undercover/pages/index/index?room_id=' + that.data.jump + '&type=2',
            })
          }
          //掷色子    如果路径是从游戏页面调过来授权的
          if (that.data.jump_dice != '') { //分享链接进入的
            wx.navigateTo({
              url: '/pages/shakedice/pages/index/index?room_id=' + that.data.jump_dice + '&type=2',
            })
          }
        }
      })
      //
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    } else {
      console.log("您尚未授予权限")
    }
  },
  //跳转谁是卧底
  goToRoom1() {
    wx.request({
      url: app.globalData.path + 'create_room',
      type: 'get',
      header: {
        'Cookie': "PHPSESSID=" + app.globalData.session_id
      },
      success: function(res) {
        if (res.data.status == 1) {
          app.globalData.isHolder=true;
          wx.navigateTo({
            url: '/pages/undercover/pages/index/index?room_id=' + res.data.room_id,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            image: "../../../../images/fail.png",
            duration: 2000
          });
        }
      },
      error: function() {
        console.log('ajax请求失败');
      }
    })
  },
  // 跳转你比我猜游戏
  goToRoom2() {
    wx.navigateTo({
      url: "/pages/guessword/pages/index/index",
    })
  },
  // 跳转掷骰子游戏页面
  goToRoom3() {
    wx.request({
      url: app.globalData.path + 'create_room',
      type: 'get',
      header: {
        'Cookie': "PHPSESSID=" + app.globalData.session_id
      },
      success: function (res) {
        if (res.data.status == 1) {
          app.globalData.isMaster = true;
          wx.navigateTo({
            url: '/pages/shakedice/pages/index/index?room_id=' + res.data.room_id,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            image: "../../../../images/fail.png",
            duration: 2000
          });
        }
      },
      error: function () {
        console.log('ajax请求失败');
      }
    })
  }
})